﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using SerbinAV.Components;
using SerbinAV.MobileSales.Sales.Entities;

namespace SerbinAV.MobileSales.Sales.Views
{

    public partial class SettingsView : Form, ISettingsView
    {
        public SettingsView()
        {
            InitializeComponent();
        }

        public new bool Show()
        {
            return (this.ShowDialog() == DialogResult.OK);
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            SaveChanges(null, null);
            DialogResult = DialogResult.OK;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            CancelChanges(null, null);
            DialogResult = DialogResult.Cancel;

        }

        private string selectPath()
        {
            OpenFileDialogEx openFileDialog = new OpenFileDialogEx(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), true);
            openFileDialog.Text = "Вкажіть шлях";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                return openFileDialog.SelectedFullName;
            }
            return null;
        }

        private string selectFile(string caption)
        {
            OpenFileDialogEx openFileDialog = new OpenFileDialogEx(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), "*.sdf", false);
            openFileDialog.Text = caption;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                return openFileDialog.SelectedFullName;
            }
            return null;
        }

        private void pathDatabaseButton_Click(object sender, EventArgs e)
        {
            string path = selectFile("Вкажіть шлях до БД");
            txtBoxPathDatabase.Text = path ?? string.Empty;

        }

        private void importPathButton_Click(object sender, EventArgs e)
        {
            string path = selectPath();
            txtBoxPathImport.Text = path ?? string.Empty;
        }

        private void exportPathButton_Click(object sender, EventArgs e)
        {
            string path = selectPath();
            txtBoxPathExport.Text = path ?? string.Empty;
        }

        #region ISettingsForm Members

        public string CompanyName
        {
            get
            {
                return txtBoxCompanyName.Text;
            }
            set
            {
                txtBoxAgentName.Text = value;
            }
        }

        public string AgentName
        {
            get
            {
                return txtBoxAgentName.Text;
            }
            set
            {
                txtBoxAgentName.Text = value;
            }
        }


        public string DbPath
        {
            get
            {
                return txtBoxPathDatabase.Text;
            }
            set
            {
                txtBoxPathDatabase.Text = value;
            }
        }

        public string DbPassword
        {
            get
            {
                return txtBoxPasswordDatabase.Text;
            }
            set
            {
                txtBoxPasswordDatabase.Text = value;
            }
        }
        public string ImportPath
        {
            get
            {
                return txtBoxPathImport.Text;
            }
            set
            {
                txtBoxPathImport.Text = value;
            }
        }

        public string ExportPath
        {
            get
            {
                return txtBoxPathExport.Text;
            }
            set
            {
                txtBoxPathExport.Text = value;
            }
        }

        public bool IsSaveFile
        {
            get
            {
                return chkBoxIsSave.Checked;
            }
            set
            {
                chkBoxIsSave.Checked = value;
            }
        }

        public bool VisibleNullGoods
        {
            get
            {
                return chkBoxVisibleNullGoods.Checked;
            }
            set
            {
                chkBoxVisibleNullGoods.Checked = value;
            }
        }

        public bool CheckRemainingGoods
        {
            get
            {
                return chkBoxCheckRemainingGoods.Checked;
            }
            set
            {
                chkBoxCheckRemainingGoods.Checked = value;
            }
        }

        public bool IsVanSales
        {
            get
            {
                return chkBoxVanSales.Checked;
            }
            set
            {
                chkBoxVanSales.Checked = value;
            }
        }

        public string FtpUrl
        {
            get
            {
                return txtBoxFtpUrl.Text;
            }
            set
            {
                txtBoxFtpUrl.Text = value;
            }
        }

        public int FtpPort
        {
            get
            {
                return (int)numUpDownFtpPort.Value;
            }
            set
            {
                numUpDownFtpPort.Value = value;
            }
        }

        public string FtpLogin
        {
            get
            {
                return txtBoxFtpLogin.Text;
            }
            set
            {
                txtBoxFtpLogin.Text = value;
            }
        }

        public string FtpPassword
        {
            get
            {
                return txtBoxFtpPassword.Text;
            }
            set
            {
                txtBoxFtpPassword.Text = value;
            }
        }

        public bool FtpIsPasiveMode
        {
            get
            {
                return chkBoxFtpPasivemode.Checked;
            }
            set
            {
                chkBoxFtpPasivemode.Checked = value;
            }
        }

        public string FtpImportPath
        {
            get { return txtBoxFtpImportPath.Text; }
            set { txtBoxFtpImportPath.Text = value; }
        }

        public string FtpExportPath
        {
            get { return txtBoxFtpExportPath.Text; }
            set { txtBoxFtpExportPath.Text = value; }
        }

        public List<Sklad> sklads
        {
            set
            {
                comBoxSklad.DataSource = value;
                comBoxSklad.DisplayMember = "NameSklad";
                comBoxSklad.ValueMember = "IdSklad";
            }
        }

        public int CurrentSklad
        {
            get
            {
                if (comBoxSklad.SelectedValue != null)
                return (int)comBoxSklad.SelectedValue;
                return 0;
            }
            set
            {
                comBoxSklad.SelectedValue = value;
            }
        }

        public bool AutoGenerateNumber
        {
            get { return chkBoxAutoGenerateDocNumber.Checked; }
            set { chkBoxAutoGenerateDocNumber.Checked = value; }
        }

        public int NumberDocument
        {
            get
            {
                return (int)numUpDownCurrentNumber.Value;
            }
            set { numUpDownCurrentNumber.Value = value; }
        }

        public bool EnableGps
        {
            get
            {
                return chkBoxEnableGps.Checked;
            }
            set
            {
                chkBoxEnableGps.Checked = value;
            }
        }

        //public string GpsTestData
        //{
        //    set
        //    {
        //        gpsDataLabel.Text = value;
        //    }
        //}

        public string PrintPath
        {
            get { return txtBoxPathPrint.Text; }
            set { txtBoxPathPrint.Text = value; }
        }
        public string ExecuteProgramPrint
        {
            get { return txtBoxExecProgramPath.Text; }
            set { txtBoxExecProgramPath.Text = value; }
        }

        public event EventHandler<EventArgs> TestGps;

        public event EventHandler<EventArgs> SaveChanges;

        public event EventHandler<EventArgs> CancelChanges;

        #endregion

        private void vanSalesCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            chkBoxCheckRemainingGoods.Checked = chkBoxVanSales.Checked;
            chkBoxCheckRemainingGoods.Enabled = !chkBoxVanSales.Checked;
        }

        private void autoGenerateDocNumbercheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            lblFtpPort.Enabled = chkBoxAutoGenerateDocNumber.Checked;
            numUpDownCurrentNumber.Enabled = chkBoxAutoGenerateDocNumber.Checked;
        }

        private void TestGPS_Click(object sender, EventArgs e)
        {
            TestGps(null, null);
        }

        private void isExecProgramCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            if (chkBoxIsExecProgram.Checked)
            {
                txtBoxExecProgramPath.Enabled = true;
            }
            else
            {
                txtBoxExecProgramPath.Text = string.Empty;
                txtBoxExecProgramPath.Enabled = false;
            }
        }

        private void pathPrintSelectButton_Click(object sender, EventArgs e)
        {
            string path = selectPath();
            txtBoxPathPrint.Text = path ?? string.Empty;
        }

        private void execProgramPathButton_Click(object sender, EventArgs e)
        {
            string path = selectFile("Вкажіть шлях до програми");
            txtBoxPathDatabase.Text = path != null ? path : string.Empty;

        }

        #region IView Members


        public void ShowAlert(string message, string capton)
        {
            MessageBox.Show(message, capton);
        }

        #endregion

        private void lblFtpPassword_ParentChanged(object sender, EventArgs e)
        {

        }
    }
}